package com.miaoquan.mqapitbk.domain.builder;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.miaoquan.mqmbg.model.TbkProduct;
import com.taobao.api.response.TbkDgMaterialOptionalResponse;
import com.taobao.api.response.TbkDgOptimusMaterialResponse;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Fa
 * @date 2020-03-29 19:44
 */
public class TbkProductBuilder extends BaseTbkProductBuilder {

    private TbkProduct tbkProduct = new TbkProduct();

    /**
     * 通过类<code>TbkDgOptimusMaterialResponse.MapData</code>对象建造淘宝客商品对象
     *
     * @param mapData 物料精选商品数据
     * @return 通用淘宝客商品对象
     */
    @Override
    public TbkProduct buildByOptimusMaterialMapData(TbkDgOptimusMaterialResponse.MapData mapData) {
        tbkProduct.setItemId(mapData.getItemId());
        tbkProduct.setTitle(mapData.getTitle());
        tbkProduct.setSalePrice(mapData.getZkFinalPrice());
        tbkProduct.setShopTitle(mapData.getShopTitle());
        tbkProduct.setCategoryId(mapData.getCategoryId());
        tbkProduct.setCouponStartFee(mapData.getCouponStartFee());
        tbkProduct.setCouponTotalCount(mapData.getCouponTotalCount());
        tbkProduct.setUserType(mapData.getUserType());
        tbkProduct.setCouponRemainCount(mapData.getCouponRemainCount());
        tbkProduct.setCommissionRate(mapData.getCommissionRate());
        tbkProduct.setCouponStartTime(mapData.getCouponStartTime());
        tbkProduct.setItemDescription(mapData.getItemDescription());
        tbkProduct.setSellerId(mapData.getSellerId());
        tbkProduct.setVolume(mapData.getVolume());
        tbkProduct.setCouponEndTime(mapData.getCouponEndTime());
        tbkProduct.setLevelOneCategoryName(mapData.getLevelOneCategoryName());
        tbkProduct.setLevelOneCategoryId(mapData.getLevelOneCategoryId());
        tbkProduct.setCategoryName(mapData.getCategoryName());
        tbkProduct.setWhiteImage(mapData.getWhiteImage());
        tbkProduct.setShortTitle(mapData.getShortTitle());
        tbkProduct.setCouponInfo(mapData.getCouponInfo());
        tbkProduct.setNick(mapData.getNick());
        tbkProduct.setReservePrice(mapData.getReservePrice());

        this.buildCouponAmount(mapData.getCouponAmount());
        this.buildClickUrl(mapData.getClickUrl());
        this.buildCouponPrice();
        this.buildSmallImages(mapData.getSmallImages());
        this.buildPictUrl(mapData.getPictUrl());
        this.buildCouponShareUrl(mapData.getCouponShareUrl());

        return tbkProduct;
    }


    @Override
    public TbkProduct buildByOptimusMaterialMapData(TbkDgMaterialOptionalResponse.MapData mapData) {
        tbkProduct.setItemId(mapData.getItemId());
        tbkProduct.setTitle(mapData.getTitle());
        tbkProduct.setSalePrice(mapData.getZkFinalPrice());
        tbkProduct.setCouponAmount(mapData.getCouponAmount());
        tbkProduct.setShopTitle(mapData.getShopTitle());
        tbkProduct.setCategoryId(mapData.getCategoryId());
        tbkProduct.setCouponStartFee(mapData.getCouponStartFee());
        tbkProduct.setCouponTotalCount(mapData.getCouponTotalCount());
        tbkProduct.setUserType(mapData.getUserType());
        tbkProduct.setCouponRemainCount(mapData.getCouponRemainCount());
        tbkProduct.setCommissionRate(mapData.getCommissionRate());
        tbkProduct.setCouponStartTime(mapData.getCouponStartTime());
        tbkProduct.setItemDescription(mapData.getItemDescription());
        tbkProduct.setSellerId(mapData.getSellerId());
        tbkProduct.setVolume(mapData.getVolume());
        tbkProduct.setCouponEndTime(mapData.getCouponEndTime());
        tbkProduct.setLevelOneCategoryName(mapData.getLevelOneCategoryName());
        tbkProduct.setLevelOneCategoryId(mapData.getLevelOneCategoryId());
        tbkProduct.setCategoryName(mapData.getCategoryName());
        tbkProduct.setWhiteImage(mapData.getWhiteImage());
        tbkProduct.setShortTitle(mapData.getShortTitle());
        tbkProduct.setCouponInfo(mapData.getCouponInfo());
        tbkProduct.setNick(mapData.getNick());
        tbkProduct.setReservePrice(mapData.getReservePrice());

        this.buildClickUrl(mapData.getUrl());
        this.buildCouponPrice();
        this.buildSmallImages(mapData.getSmallImages());
        this.buildPictUrl(mapData.getPictUrl());
        this.buildCouponShareUrl(mapData.getCouponShareUrl());

        return tbkProduct;
    }

    private void buildSmallImages(List<String> smallImages) {
        tbkProduct.setSmallImages(CollectionUtil.join(smallImages, ","));
    }

    private void buildCouponShareUrl(String couponShareUrl) {
        if (!StringUtils.isEmpty(couponShareUrl)) {
            tbkProduct.setCouponShareUrl(
                    couponShareUrl.substring(0, Math.min(500, couponShareUrl.length()))
            );
        }
    }

    private void buildPictUrl(String pictUrl) {
        if (!StringUtils.isEmpty(pictUrl)) {
            tbkProduct.setPictUrl(pictUrl.substring(0, Math.min(500, pictUrl.length())));
        }
    }

    private void buildCouponPrice() {
        if (!StringUtils.isEmpty(tbkProduct.getCouponAmount())) {
            tbkProduct.setCouponPrice(
                    NumberUtil.sub(
                            tbkProduct.getSalePrice(), tbkProduct.getCouponAmount()
                    ).toPlainString()
            );
        } else {
            tbkProduct.setCouponPrice(tbkProduct.getSalePrice());
        }
    }

    private void buildClickUrl(String clickUrl) {
        if (!StringUtils.isEmpty(clickUrl)) {
            tbkProduct.setClickUrl(clickUrl.substring(0, Math.min(500, clickUrl.length())));
        }
    }

    private void buildCouponAmount(Long couponAmount) {
        tbkProduct.setCouponAmount(couponAmount != null ? String.valueOf(couponAmount) : "");
    }

}
